package com.ebt.m.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.ebt.m.AppInitService;
import com.ebt.m.commons.buscomponent.listview.BaseListContract;
import com.ebt.m.commons.buscomponent.listview.BaseListPresenter;
import com.ebt.m.commons.buscomponent.listview.RecyclerFootManger;
import com.ebt.m.commons.buscomponent.listview.SectionItem;
import com.ebt.m.commons.utils.Logger;
import com.ebt.m.commons.utils.NetUtils;
import com.ebt.m.commons.utils.RxUtil;
import com.ebt.m.data.rxModel.api.EBTAPI;
import com.ebt.m.data.rxModel.apibean.OpportunityDataResult;
import com.ebt.m.utils.ConfigData;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by andy.wang on 2016/12/14.
 */

public class OpportunityPresenter extends BaseListPresenter<BaseListContract.IView> {

    public static final int TYPE_PROPOSAL = 1;
    public static final int TYPE_ALERT = 2;
    public static final int TYPE_CLOUD = 3;

    private int currentPage = 1;

    public OpportunityPresenter(Context context, BaseListContract.IView iView) {
        super(context, iView);
    }

    @Override
    public void loadNew(Object... args) {
        currentPage = 1;
        EBTAPI opportunityApi = AppInitService.getEbtApi();
        opportunityApi.getOpportunities(currentPage, ConfigData.PAGE_COUNT_LIMIT)
                .filter(getFilterForFootviewStatus(true))
                .compose(RxUtil.rxTransformer(iView))
                .subscribe(response -> iView.updateData(createSectionItem(response.getData()))
                        , throwable -> {
                            throwable.printStackTrace();
                            NetUtils.checkHttpException(mContext, throwable);
                            iView.errorData();
                        });
    }

    @Override
    public void refresh() {
        loadNew();
    }

    @Override
    public void loadNext(Object... args) {
        EBTAPI opportunityApi = AppInitService.getEbtApi();
        opportunityApi.getOpportunities(currentPage + 1, ConfigData.PAGE_COUNT_LIMIT)
                .filter(getFilterForFootviewStatus(false))
               .compose(RxUtil.rxTransformer(iView))
                .subscribe(response -> {
                    currentPage++;
                    iView.updateDataAdd(createSectionItem(response.getData()));
                }, throwable -> {
                    throwable.printStackTrace();
                    NetUtils.checkHttpException(mContext, throwable);
                    iView.setFootStatus(RecyclerFootManger.STATUS_ERROR, true);
                });
    }

    @Override
    public List<SectionItem> createSectionItem(List list) {
        List<SectionItem> sections = new ArrayList<SectionItem>();
        if (list == null || list.size() < 1) {
            return sections;
        }
        try {
            for (int i = 0; i < list.size(); i++) {
                OpportunityDataResult opportunityDataResult = (OpportunityDataResult) list.get(i);
                SectionItem item2 = new SectionItem(opportunityDataResult.getType(), opportunityDataResult);
                sections.add(item2);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return sections;
        }
        return sections;
    }

    public void readMessage(int id) {
        EBTAPI opportunityApi = AppInitService.getEbtApi();
        opportunityApi.readMessage(id)
                .compose(RxUtil.rxTransformer(iView))
                .subscribe(response -> {
                    try {
                        if (!TextUtils.isEmpty(response.body().string())) {
                        }
                    } catch (Exception e) {
                        Logger.e(e);
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                    NetUtils.checkHttpException(mContext, throwable);
                });
    }
}
